<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="adviceName">
      <el-input v-model="dataForm.adviceName" placeholder="留言人名称"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="advicePhone">
      <el-input v-model="dataForm.advicePhone" placeholder="留言人电话"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="adviceEmail">
      <el-input v-model="dataForm.adviceEmail" placeholder="留言人邮箱"></el-input>
    </el-form-item>
    <el-form-item label="详细需求" prop="adviceContent">
      <el-input v-model="dataForm.adviceContent" placeholder="留言人的详细需求"></el-input>
    </el-form-item>
    <!-- <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item> -->
    <!-- <el-form-item label="地址" prop="adviceIp">
      <el-input v-model="dataForm.adviceIp" placeholder="地址"></el-input>
    </el-form-item> -->
    <el-form-item label="留言状态" prop="adviceStatus">
    <el-select v-model="dataForm.adviceStatus" placeholder="留言状态 0：未处理 1：已处理">
					<el-option label="未处理" value="0"></el-option>
					<el-option label="已处理" value="1"></el-option>
				</el-select>
    </el-form-item>
    <!-- <el-form-item label="留言备注" prop="adviceRemark">
      <el-input v-model="dataForm.adviceRemark" placeholder="留言备注"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          adviceId: 0,
          adviceName: '',
          advicePhone: '',
          adviceEmail: '',
          adviceContent: '',
          createTime: '',
          updateTime: '',
          adviceIp: '',
          adviceStatus: '',
          adviceRemark: ''
        },
        dataRule: {
          adviceName: [
            { required: true, message: '留言人名称不能为空', trigger: 'blur' }
          ],
          advicePhone: [
            { required: true, message: '留言人电话不能为空', trigger: 'blur' }
          ],
          adviceEmail: [
            { required: true, message: '留言人邮箱不能为空', trigger: 'blur' }
          ],
          adviceContent: [
            { required: true, message: '留言人的详细需求不能为空', trigger: 'blur' }
          ],
          // createTime: [
          //   { required: true, message: '创建时间不能为空', trigger: 'blur' }
          // ],
          // updateTime: [
          //   { required: true, message: '更新时间不能为空', trigger: 'blur' }
          // ],
          // adviceIp: [
          //   { required: true, message: '地址不能为空', trigger: 'blur' }
          // ],
          adviceStatus: [
            { required: true, message: '留言状态 0：未处理 1：已处理不能为空', trigger: 'blur' }
          ],
          adviceRemark: [
            { required: true, message: '留言备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.adviceId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.adviceId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/webadvice/info/${this.dataForm.adviceId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.adviceName = data.webAdvice.adviceName
                this.dataForm.advicePhone = data.webAdvice.advicePhone
                this.dataForm.adviceEmail = data.webAdvice.adviceEmail
                this.dataForm.adviceContent = data.webAdvice.adviceContent
                this.dataForm.createTime = data.webAdvice.createTime
                this.dataForm.updateTime = data.webAdvice.updateTime
                this.dataForm.adviceIp = data.webAdvice.adviceIp
                this.dataForm.adviceStatus = data.webAdvice.adviceStatus
                this.dataForm.adviceRemark = data.webAdvice.adviceRemark
                
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/webadvice/${!this.dataForm.adviceId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'adviceId': this.dataForm.adviceId || undefined,
                'adviceName': this.dataForm.adviceName,
                'advicePhone': this.dataForm.advicePhone,
                'adviceEmail': this.dataForm.adviceEmail,
                'adviceContent': this.dataForm.adviceContent,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'adviceIp': this.dataForm.adviceIp,
                'adviceStatus': this.dataForm.adviceStatus,
                'adviceRemark': this.dataForm.adviceRemark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
